package com.padakeji.android.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

import com.padakeji.android.ui.R;

/**
 * Created by atanl on 2015/9/29.
 */
public class IconEditText extends EditText {

    private final String TAG = this.getClass().getName();

    private enum Drawables {
        LEFT,
        TOP,
        RIGHT,
        BOTTOM,
    }

    private static final int DEFAULT_DIMENSION = -1;

    public interface OnDrawableLeftClickListener {
        void onDrawableLeftClick();
    }

    public interface OnDrawableTopClickListener {
        void onDrawableTopClick();
    }

    public interface OnDrawableRightClickListener {
        void onDrawableRightClick();
    }

    public interface OnDrawableBottomClickListener {
        void onDrawableBottomClick();
    }

    private int drawableLeftWidth;
    private int drawableLeftHeight;
    private int drawableTopWidth;
    private int drawableTopHeight;
    private int drawableRightWidth;
    private int drawableRightHeight;
    private int drawableBottomWidth;
    private int drawableBottomHeight;

    private OnDrawableLeftClickListener onDrawableLeftClickListener;
    private OnDrawableTopClickListener onDrawableTopClickListener;
    private OnDrawableRightClickListener onDrawableRightClickListener;
    private OnDrawableBottomClickListener onDrawableBottomClickListener;


    public IconEditText(Context context) {
        super(context);
    }

    public IconEditText(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.IconEditTextStyle); // 暂时不知道怎么解决其他主题中不定义IconEditTextStyle时不能渲染的问题。
    }

    public IconEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconEditText, defStyleAttr, R.style.IconEditTextDefaultStyle);

        drawableTopWidth = a.getDimensionPixelSize(R.styleable.IconEditText_drawableTopWidth, DEFAULT_DIMENSION);
        drawableTopHeight = a.getDimensionPixelSize(R.styleable.IconEditText_drawableTopHeight, DEFAULT_DIMENSION);
        drawableRightWidth = a.getDimensionPixelSize(R.styleable.IconEditText_drawableRightWidth, DEFAULT_DIMENSION);
        drawableRightHeight = a.getDimensionPixelSize(R.styleable.IconEditText_drawableRightHeight, DEFAULT_DIMENSION);
        drawableBottomWidth = a.getDimensionPixelSize(R.styleable.IconEditText_drawableBottomWidth, DEFAULT_DIMENSION);
        drawableBottomHeight = a.getDimensionPixelSize(R.styleable.IconEditText_drawableBottomHeight, DEFAULT_DIMENSION);
        drawableLeftWidth = a.getDimensionPixelSize(R.styleable.IconEditText_drawableLeftWidth, DEFAULT_DIMENSION);
        drawableLeftHeight = a.getDimensionPixelSize(R.styleable.IconEditText_drawableLeftHeight, DEFAULT_DIMENSION);

        a.recycle();

        resizeCompoundDrawables();
    }


    private void resizeCompoundDrawables() {
        Drawable[] ds = getCompoundDrawables();
        resizeCompoundDrawables(ds[0], ds[1], ds[2], ds[3]);
    }


    private void resizeCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        if (left != null) {
            if (drawableLeftWidth != -1 && drawableLeftHeight != -1) {
                left.setBounds(0, 0, drawableLeftWidth, drawableLeftHeight);
            }
            Rect leftRect = left.getBounds();
            drawableLeftWidth = leftRect.width();
            drawableLeftHeight = leftRect.height();
        }
        if (top != null) {
            if (drawableTopWidth != -1 && drawableTopHeight != -1) {
                top.setBounds(0, 0, drawableTopWidth, drawableTopHeight);
            }
            Rect topRect = top.getBounds();
            drawableTopWidth = topRect.width();
            drawableTopHeight = topRect.height();
        }
        if (right != null) {
            if (drawableRightWidth != -1 && drawableRightHeight != -1) {
                right.setBounds(0, 0, drawableRightWidth, drawableRightHeight);
            }
            Rect rightRect = right.getBounds();
            drawableRightWidth = rightRect.width();
            drawableRightHeight = rightRect.height();
        }
        if (bottom != null) {
            if (drawableBottomWidth != -1 && drawableBottomHeight != -1) {
                bottom.setBounds(0, 0, drawableBottomWidth, drawableBottomHeight);
            }
            Rect bottomRect = bottom.getBounds();
            drawableBottomWidth = bottomRect.width();
            drawableBottomHeight = bottomRect.height();
        }

        super.setCompoundDrawables(left, top, right, bottom);
    }


    private int getDimensionWithDefaultValue(int dimension) {
        int value = dimension;
        if (value < 0) {
            value = DEFAULT_DIMENSION;
        }
        return value;
    }

    @Override
    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        super.setCompoundDrawables(left, top, right, bottom);
        resizeCompoundDrawables();
    }

    public void setDrawableLeftWidth(int drawableLeftWidth) {
        this.drawableLeftWidth = getDimensionWithDefaultValue(drawableLeftWidth);
        resizeCompoundDrawables();
    }

    public void setDrawableLeftHeight(int drawableLeftHeight) {
        this.drawableLeftHeight = getDimensionWithDefaultValue(drawableLeftHeight);
        resizeCompoundDrawables();
    }

    public void setDrawableTopWidth(int drawableTopWidth) {
        this.drawableTopWidth = getDimensionWithDefaultValue(drawableTopWidth);
        resizeCompoundDrawables();
    }

    public void setDrawableTopHeight(int drawableTopHeight) {
        this.drawableTopHeight = getDimensionWithDefaultValue(drawableTopHeight);
        resizeCompoundDrawables();
    }

    public void setDrawableRightWidth(int drawableRightWidth) {
        this.drawableRightWidth = getDimensionWithDefaultValue(drawableRightWidth);
        resizeCompoundDrawables();
    }

    public void setDrawableRightHeight(int drawableRightHeight) {
        this.drawableRightHeight = getDimensionWithDefaultValue(drawableRightHeight);
        resizeCompoundDrawables();
    }


    public void setDrawableBottomWidth(int drawableBottomWidth) {
        this.drawableBottomWidth = getDimensionWithDefaultValue(drawableBottomWidth);
        resizeCompoundDrawables();
    }

    public void setDrawableBottomHeight(int drawableBottomHeight) {
        this.drawableBottomHeight = getDimensionWithDefaultValue(drawableBottomHeight);
        resizeCompoundDrawables();
    }


    public void setOnDrawableLeftClickListener(OnDrawableLeftClickListener onDrawableLeftClickListener) {
        this.onDrawableLeftClickListener = onDrawableLeftClickListener;
    }

    public void setOnDrawableTopClickListener(OnDrawableTopClickListener onDrawableTopClickListener) {
        this.onDrawableTopClickListener = onDrawableTopClickListener;
    }

    public void setOnDrawableRightClickListener(OnDrawableRightClickListener onDrawableRightClickListener) {
        this.onDrawableRightClickListener = onDrawableRightClickListener;
    }

    public void setOnDrawableBottomClickListener(OnDrawableBottomClickListener onDrawableBottomClickListener) {
        this.onDrawableBottomClickListener = onDrawableBottomClickListener;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean superResult = super.onTouchEvent(event);

        int action = event.getActionMasked();

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                return superResult;
            case MotionEvent.ACTION_UP:
                int actionIndex = event.getActionIndex();
                Drawables clickedD = getClickedDrawable(event.getX(actionIndex), event.getY(actionIndex));
                if (clickedD == null) {
                    return superResult;
                }
                switch (clickedD) {
                    case LEFT:
                        if (onDrawableLeftClickListener != null) {
                            onDrawableLeftClickListener.onDrawableLeftClick();
                            return true;
                        } else {
                            return superResult;
                        }
                    case TOP:
                        if (onDrawableTopClickListener != null) {
                            onDrawableTopClickListener.onDrawableTopClick();
                            return true;
                        } else {
                            return superResult;
                        }
                    case RIGHT:
                        if (onDrawableRightClickListener != null) {
                            onDrawableRightClickListener.onDrawableRightClick();
                            return true;
                        } else {
                            return superResult;
                        }
                    case BOTTOM:
                        if (onDrawableBottomClickListener != null) {
                            onDrawableBottomClickListener.onDrawableBottomClick();
                            return true;
                        } else {
                            return superResult;
                        }
                    default:
                        return true;
                }
            default:
                break;
        }

        return superResult;

    }


    private Drawables getClickedDrawable(float x, float y) {
        int w = getWidth();
        int h = getHeight();
        if (x > getPaddingLeft() && x < drawableLeftWidth + getPaddingLeft()
                && y > (h + getPaddingTop() - getPaddingBottom() + drawableTopHeight - drawableBottomHeight - drawableLeftHeight)/2
                && y < (h + getPaddingTop() - getPaddingBottom() + drawableTopHeight - drawableBottomHeight + drawableLeftHeight)/2)
        {
            return Drawables.LEFT;
        }
        if (x > (w + getPaddingLeft() + getPaddingRight() + drawableLeftWidth - drawableRightWidth - drawableTopWidth)/2
                && x < (w + getPaddingLeft() + getPaddingRight() + drawableLeftWidth - drawableRightWidth + drawableTopWidth)/2
                && y > getPaddingTop() && y < getPaddingTop() + drawableTopHeight) {
            return Drawables.TOP;
        }
        if (x > getWidth() - getPaddingRight() - drawableRightWidth
                && x < getWidth() - getPaddingRight()
                && y > (h + getPaddingTop() - getPaddingBottom() + drawableTopHeight - drawableBottomHeight - drawableRightHeight)/2
                && y < (h + getPaddingTop() - getPaddingBottom() + drawableTopHeight - drawableBottomHeight + drawableRightHeight)/2)
        {
            return Drawables.RIGHT;
        }
        if (x > (w + getPaddingLeft() - getPaddingRight() + drawableLeftWidth - drawableRightWidth - drawableBottomWidth)/2
                && x < (w + getPaddingLeft() - getPaddingRight() + drawableLeftWidth - drawableRightWidth + drawableBottomWidth)/2
                && y > h - getPaddingBottom() - drawableBottomHeight && y < h - getPaddingBottom())
        {
            return Drawables.BOTTOM;
        }

        return null;
    }


}
